package com.atguigu.collection;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

public class NotSafeCollection {
    public static void main(String[] args) throws InterruptedException {
//        NotSaveList();
        NotSaveSet();
//        NotSaveMap();
    }

    public static void NotSaveList() throws InterruptedException {
        ArrayList<Integer> list = new ArrayList<>();
        List<Integer> list1 = new Vector<>();
        List<Integer> list2 = Collections.synchronizedList(new ArrayList<>());
        List<Integer> list3 = new CopyOnWriteArrayList<>();

        for (int i = 0; i < 30; i++) {
            int finalI = i;
            new Thread(() -> {
                list3.add(finalI);
                System.out.println("list = " + list3);
            }).start();
            Thread.sleep(100);
        }
    }

    public static void NotSaveSet() {
        Set<Integer> set = new HashSet();
        Set<Integer> set1 = Collections.synchronizedSet(new HashSet<>());
        Set<Integer> set2 = new CopyOnWriteArraySet<>();


        for (int i = 0; i < 30; i++) {
            int fi = i;
            new Thread(() -> {
                set2.add(fi);
                System.out.println("set = " + set2);
                ;
            }).start();
        }
    }

    public static void NotSaveMap() {
        Map<Integer, Object> map = new HashMap<>();
        Map<Integer, Object> map1 = Collections.synchronizedMap(new HashMap<>());
        Map<Integer, Object> map2 = new ConcurrentHashMap<>();

        for (int i = 0; i < 30; i++) {
            int fi = i;
            new Thread(() -> {
                map2.put(fi, fi + "666");
                System.out.println("map = " + map2);
            }).start();
        }
    }
}
